/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>HServicePayRebackItemServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年11月16日 下午9:43:49<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HServiceOrderUpdateApplyMapper;
import com.flea.hclv.model.dao.HServiceOrderUpdateRecoderMapper;
import com.flea.hclv.model.entity.HServiceOrderUpdateApply;
import com.flea.hclv.model.entity.HServiceOrderUpdateRecoder;
import com.flea.hclv.service.HServiceOrderUpdateApplyService;
import com.flea.system.base.util.StringUtil;
import com.flea.system.utils.FleaStringUtils;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.flea.hclv.util.ServerResponse;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.model.dao.HServicePayRebackItemMapper;
import com.flea.hclv.model.entity.HServicePayRebackItem;
import com.flea.hclv.service.HServicePayRebackItemService;
import com.flea.system.bean.FleaPage;
import com.flea.system.platform.session.FleaOrgUtils;

/**
 * @description 退款信息查看，等记录服务
 * @createTime 2018年11月16日 下午9:43:49
 * @modifyTime 
 * @author myflea@163.com
 * @version 1.0
 */
@Service
public class HServicePayRebackItemServiceImpl implements HServicePayRebackItemService {
	@Autowired
	private HServicePayRebackItemMapper hServicePayRebackItemMapper;
	@Autowired
	private HServiceOrderUpdateApplyService hServiceOrderUpdateApplyService;
	@Autowired
	private HServiceOrderUpdateApplyMapper hServiceOrderUpdateApplyMapper;
	@Override
	public ServerResponse<FleaPage<HServicePayRebackItem>> findByOrgIdForPaging(
		
			HttpServletRequest request, int curPage, int pageSize,
			String orderNo, String rebackStatus) {
		String payOrgId = FleaOrgUtils.getCurrentUserDeptUuid(request);
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		List<HServicePayRebackItem> lists = hServicePayRebackItemMapper.findByPayOrgIdAndOrdNoAndRebackStatusPaging(rowBounds,payOrgId,orderNo,rebackStatus);
		FleaPage<HServicePayRebackItem> result = new FleaPage<HServicePayRebackItem>(lists);
		return ServerResponse.createBySuccess("成功", result);
	}
	@Override
	public ServerResponse<HServicePayRebackItem> updateRebackRecoder(
			HttpServletRequest request, String model) throws Exception {
		HServicePayRebackItem item = JSONObject.parseObject(model, HServicePayRebackItem.class);
		if(item==null){
			return ServerResponse.createByErrorMessage("支付信息解析失败，请重试");
		}
			//支付方式，支付状态，支付流水号
			String rebackId = item.getId();
			
			HServicePayRebackItem result = 	hServicePayRebackItemMapper.selectByPrimaryKey(rebackId);
			if(null==result){
				return ServerResponse.createByErrorMessage("退款记录不存在");
			}
			if(result.getPlatformStatus()=="退款完成"){
				return ServerResponse.createByErrorMessage("退款记录不存在");
			}
			
			
			
		
		
		return null;
	}

  @Override
  public ServerResponse addServiceOrderRecbackRecoder(String model, HttpServletRequest request)
      throws Exception {
	  if(StringUtil.isEmpty(model)){
	    return ServerResponse.createByErrorMessage("参数为空");
    }
    JSONObject jsb = JSONObject.parseObject(model);
    if(null==jsb){
      return ServerResponse.createByErrorMessage("参数转版失败");
    }

    String updateApplyId = jsb.getString("updateApplyId");
    if(StringUtil.isEmpty(updateApplyId)){
      return ServerResponse.createByErrorMessage("updateApplyId参数为空");
    }
    String rebackString = jsb.getString("rebackRecoder");
    if(StringUtil.isEmpty(rebackString)){
      return ServerResponse.createByErrorMessage("rebackRecoder参数为空");
    }
    HServicePayRebackItem recoder = JSONObject.parseObject(rebackString,HServicePayRebackItem.class);
    if(null==recoder){
      return ServerResponse.createByErrorMessage("rebackRecoder参数转版为空");
    }

    ServerResponse result =  hServiceOrderUpdateApplyService.findByApperId(updateApplyId);
    if(!result.isSuccess()){
      return ServerResponse.createByErrorMessage(result.getMessage());
    }
    Object applyObj = result.getData();
    if(null==applyObj){
      return ServerResponse.createByErrorMessage("申请不存在");
    }
    HServiceOrderUpdateApply apply = (HServiceOrderUpdateApply)applyObj;
    if(!HConstant.D_HCLV_D_SERVICE_ORDER_UPDATE_ALLPY_TYPE_REBACK.equals(apply.getType())){
      return ServerResponse.createByErrorMessage("此申请不是退款申请，不可进行退款操作");
    }
    if(!HConstant.D_HCLV_D_SERVICE_ORDER_UPDATE_ALLPY_STATE_AGREE.equals(apply.getStatus())){
      return ServerResponse.createByErrorMessage("申请还未通过审批，暂时无法进行退款操作");
    }
    String currentOrgId = FleaOrgUtils.getCurrentUserDeptUuid(request);
    String currentUserId = FleaOrgUtils.getCurUserUuid(request);
    //由接收申请方方进行退款操作
    if(!currentOrgId.equals(apply.getReceiveOrgId())){
      return ServerResponse.createByErrorMessage("抱歉，您没有退款权限，禁止进行非法操作，以免您的账号被锁定！");
    }
    recoder.setCreateUser(currentUserId);
    recoder.setOrderNo(apply.getServerOrderNo());
    recoder.setId(FleaStringUtils.getGenerateUUID());
    recoder.setUuid(currentOrgId);
    recoder.setPayOrgId(currentOrgId);
    recoder.setReceiveId(apply.getApplerOrgId());
    boolean resultBoolean = hServicePayRebackItemMapper.insertSelective(recoder)==1;
    if(!resultBoolean){
      throw new  Exception("退款失败，请联系运维人员");
    }
    apply.setStatus(HConstant.D_HCLV_D_SERVICE_ORDER_UPDATE_ALLPY_STATE_WC);
    apply.setModifyUser(currentUserId);
    resultBoolean = hServiceOrderUpdateApplyMapper.updateByPrimaryKeySelective(apply)==1;
    if(!resultBoolean){
      throw new  Exception("退款失败，请联系运维人员");
    }
    return ServerResponse.createBySuccessMessage("退款成功");
  }

  @Override
  public ServerResponse<List<HServicePayRebackItem>> findByOrderNo(String model) throws Exception {
	  List<HServicePayRebackItem> result = hServicePayRebackItemMapper.findByOrderNo(model);
	  result = coverCodeToName(result);
    return ServerResponse.createBySuccess(result);
  }

  /**
   * 我退款给其他供应商多少钱
   * @param pagDeptId
   * @param orderNo
   * @return
   */
  @Override
  public List<HServicePayRebackItem> findByPayOrgIdAndOrderNo(String pagDeptId, String orderNo) {
    List<HServicePayRebackItem> payBackToOther = hServicePayRebackItemMapper.findByPayOrgIdAndOrderNo(pagDeptId,orderNo);
    return payBackToOther;
  }

  /**
   * 我接到多少退款
   * @param recivedOrgId
   * @param orderNo
   * @return
   */
  @Override
  public List<HServicePayRebackItem> getBackPaysByBackOrgIdAndOrderNo(String recivedOrgId,
      String orderNo) {
    List<HServicePayRebackItem> payBackToOther = hServicePayRebackItemMapper.findByRecevidOrgIdAndOrderNo(recivedOrgId,orderNo);
    return payBackToOther;
  }

  private List<HServicePayRebackItem> coverCodeToName(List<HServicePayRebackItem> data){
	  List<HServicePayRebackItem> newResult = new ArrayList<>();
    for (HServicePayRebackItem item:data
    ) {
      item.setPayOrgName(FleaOrgUtils.getOrgDeptByDeptUuid(item.getPayOrgId()).getDeptName());
      item.setReceiveName(FleaOrgUtils.getOrgDeptByDeptUuid(item.getReceiveId()).getDeptName());
      item.setCreateUserName(FleaOrgUtils.getUserByUserUuid(item.getCreateUser()).getUserName());
      newResult.add(item);
    }
	  return newResult;

  }

}
